package com.uchiha.nova.core.tool.support;

import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.constant.HeaderConstant;
import com.uchiha.nova.core.tool.util.WebUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;


public class CustomController {
    public CustomController() {
    }
    @Resource
    private HttpServletRequest request;

    /**
     * 获取request
     */
    public HttpServletRequest getRequest() {
        return this.request;
    }

    /**
     * 获取当前用户
     *
     * @return
     */
    protected NovaUser getNovaUser() {
        HttpServletRequest request = WebUtil.getRequest();
        if (request == null) {
            return null;
        }
        NovaUser novaUser = new NovaUser();
        novaUser.setUserId(Long.valueOf(request.getHeader(HeaderConstant.NOVA_USER_ID)));
        novaUser.setTenantId(request.getHeader(HeaderConstant.NOVA_USER_TENANT));

        // 主管部门
//		Org org = (Org) redisTemplate.opsForValue().get(RedisCacheConstants.PARENT_ORG + Long.valueOf(request.getHeader(HeaderConstant.CLOUDOER_USER_ID)));
//		if(ObjectUtil.isNotEmpty(org)){
//			novaUser.setOrgParentId(org.getId());
//			novaUser.setOrgParentName(org.getName());
//		}
        return novaUser;
    }

    /** ============================     API_RESULT    =================================================  */

    /**
     * 返回ApiResult
     *
     * @param data
     * @return R
     */
    public <T> R<T> data(T data) {
        return R.data(data);
    }

    /**
     * 返回ApiResult
     *
     * @param data
     * @param message
     * @return R
     */
    public <T> R<T> data(T data, String message) {
        return R.data(data, message);
    }

    /**
     * 返回ApiResult
     *
     * @param data
     * @param message
     * @param code
     * @return R
     */
    public <T> R<T> data(T data, String message, int code) {
        return R.data(code, data, message);
    }

    /**
     * 返回ApiResult
     *
     * @param message
     * @return R
     */
    public R success(String message) {
        return R.success(message);
    }

    /**
     * 返回ApiResult
     *
     * @param message
     * @return R
     */
    public R fail(String message) {
        return R.fail(message);
    }

    /**
     * 返回ApiResult
     *
     * @param flag
     * @return R
     */
    public R status(boolean flag) {
        return R.status(flag);
    }
}
